package com.fastloan.mgt.invest.controller.form;

import com.fastloan.mgt.invest.model.InvestFrom;
import com.fastloan.mgt.invest.service.InvestFromService;
import com.fastloan.safe.core.util.ManagerKit;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 题目管理
 *
 * @author rankai
 *         createTime 2017-08-2017/8/8 10:39
 */
@RestController
@RequestMapping("/api/invest/form")
public class InvestFromController extends BaseController<InvestFrom> {

    @Autowired
    private InvestFromService investFromService;

    /**
     * 获取题目分页列表
     *
     * @return
     */
    @RequestMapping("/pageList")
    public ResultTo pageList(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("topOrgId", ManagerKit.getTopOrgId());
        String tabs = request.getParameter("tabs");
        List<String> list = null;
        if (tabs != null) {
            String[] split = tabs.split(",");
            list = Arrays.asList(split);
        }
        map.put("tabs", list);
        return dataPage("investFromPageList", map);
    }

    /**
     * 根据Id获取题目
     *
     * @param investFromId 题目ID
     * @return
     */
    @GetMapping("/{investFromId}")
    public ResultTo select(@PathVariable("investFromId") String investFromId) {
        InvestFrom investFrom = investFromService.selectByPrimaryKey(investFromId);
        if (investFrom.getState() == 2) {
            return new ResultTo(600, "该数据已被删除");
        }
        return new ResultTo().setData(investFrom);
    }

    /**
     * 增加题目
     *
     * @param investFrom 题目Bean
     * @return
     */
    @PostMapping
    public ResultTo insert(InvestFrom investFrom) {
        if (!haveFrom(investFrom)) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "此题目已经存在");
        }
        investFrom.setTopOrgId(ManagerKit.getTopOrgId().toString());
        investFromService.insertSelective(investFrom);
        return new ResultTo();
    }

    /**
     * 更新题目
     *
     * @param investFrom 题目Bean
     * @return
     */
    @PutMapping
    public ResultTo update(InvestFrom investFrom) {
        if (!haveFrom(investFrom)) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "此题目已经存在");
        }
        investFrom.setTopOrgId(null);
        investFromService.updateByPrimaryKeySelective(investFrom);
        return new ResultTo();
    }

    private boolean haveFrom(InvestFrom investFrom) {
        Example example = new Example(InvestFrom.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("topOrgId")
                .andEqualTo("cname", investFrom.getCname())
                .andEqualTo("type", investFrom.getType());
        if (investFrom.getId() != null) {
            criteria.andNotEqualTo("id", investFrom.getId());
        }
        List<InvestFrom> investFroms = investFromService.selectByExample(example);
        if (investFroms.size() > 0) {
            if (investFroms.get(0).getType() == 3) {
                return false;
            } else {
                String[] split = investFroms.get(0).getOptions().split(",");
                String[] splitNew = investFrom.getOptions().split(",");
                boolean flagAll = true;
                for (int i = 0; i < split.length; i++) {
                    int flag = 0;
                    for (int n = 0; n < splitNew.length; n++) {
                        if (split[i].equals(splitNew[n])) {
                            flag = 1;
                        }
                    }
                    if (flag == 0) {
                        flagAll = false;
                        break;
                    }
                }
                if (flagAll) {
                    return false;
                }
            }
        }
        return true;
    }
}
